package org.bird.concurrent.lock;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author liangjf
 * 重入版本自旋锁样例
 */
public class SpinLock {
	private AtomicReference<Thread> sign = new AtomicReference<Thread>();

	public void lock() {

		Thread current = Thread.currentThread();
		//CAS原子操作
		while (!sign.compareAndSet(null, current)) {

		}

	}

	public void unlock() {

		Thread current = Thread.currentThread();

		sign.compareAndSet(current, null);

	}

}
